package com.zygh.policeasset.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.component.DataLabelsComponent;
import com.zygh.policeasset.component.OperLog;
import com.zygh.policeasset.pojo.Basics;
import com.zygh.policeasset.pojo.DailyAsset;
import com.zygh.policeasset.pojo.Hardwit;
import com.zygh.policeasset.pojo.Images;
import com.zygh.policeasset.pojo.Softwit;
import com.zygh.policeasset.pojo.ZipOrRar;
import com.zygh.policeasset.service.BasicsService;
import com.zygh.policeasset.service.DailyAssetsService;
import com.zygh.policeasset.service.DailyProjectService;
import com.zygh.policeasset.service.HardwitService;
import com.zygh.policeasset.service.ImagesService;
import com.zygh.policeasset.service.SoftwitService;
import com.zygh.policeasset.service.UnitService;
import com.zygh.policeasset.service.WitbigService;
import com.zygh.policeasset.service.WitsmallService;
import com.zygh.policeasset.utils.MapUtil;
import com.zygh.policeasset.utils.UnZipAndRarUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping({"/assetsfiles"})
@Api(tags = {"资产模块上传数据压缩包"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/AssetsFilesController.class */
public class AssetsFilesController {
    @Autowired
    HardwitService hardwitService;
    @Autowired
    SoftwitService softwitService;
    @Autowired
    BasicsService basicsService;
    @Autowired
    WitbigService witbigService;
    @Autowired
    WitsmallService witsmallService;
    @Autowired
    UnitService unitService;
    @Autowired
    ImagesService imagesService;
    @Autowired
    DailyAssetsService dailyAssetsService;
    @Autowired
    DailyProjectService dailyProjectService;
    @Value("${file.uploadFolderzn}")
    private String uploadFolderzn;
    @Value("${file.uploadFolderjc}")
    private String uploadFolderjc;
    @Value("${file.uploadFolderda}")
    private String uploadFolderda;

    @RequestMapping(value = {"/addRepository"}, method = {RequestMethod.POST})
    @OperLog(operModul = "资产模块上传数据压缩包", operType = "添加", operDesc = "资产模块上传数据压缩包")
    @ApiOperation("上传数据压缩包")
    public Object addRepository(@RequestBody List<MultipartFile> files, @RequestParam String type, @RequestParam(value = "projectNo", required = false) String projectNo, @RequestParam String inputNum, @RequestParam(value = "sectionName", required = false) String sectionName, @RequestParam(value = "sectionId", required = false) Integer sectionId, @RequestParam(value = "companyId", required = false) Integer companyId) throws Exception {
        String[] columns1;
        String outDir;
        String[] columns2 = null;
        String str = "";
        if (!files.isEmpty() && files.size() > 0) {
            for (MultipartFile multipartFile : files) {
                if ("zn".equals(type)) {
                    columns1 = new String[]{"assetNum", "pactName", "bigNum", "smallNum", "facilityName", "facilityIp", "startTime", "stopTime", "startMoney", "planYear", "qualityYear", "place", "images", "brandName", "brandType", "takePolice", "construcUnit", "buildUnit", "maintainUnit", "powerType", "whetherEnable", "whetherPower", "whetherNetwork", "remark", "depreciationPeriod", "createTime"};
                    columns2 = new String[]{"assetNum", "pactName", "bigNum", "smallNum", "softwitName", "softContent", "issueType", "softIp", "startTime", "stopTime", "startMoney", "planYear", "qualityYear", "images", "climax", "takePolice", "construcUnit", "buildUnit", "maintainUnit", "whetherEnable", "normalAccess", "remark", "depreciationPeriod", "createTime"};
                    outDir = this.uploadFolderzn + System.currentTimeMillis() + "/";
                } else if ("jc".equals(type)) {
                    columns1 = new String[]{"assetNum", "additionalNumber", "pactName", "bigNum", "smallNum", "basicsName", "specifica", "brand", "number", "startMoney", "money", "construcUnit", "buildUnit", "startTime", "stopTime", "planYear", "place", "remark", "images", "depreciationPeriod", "createTime"};
                    outDir = this.uploadFolderjc + System.currentTimeMillis() + "/";
                } else if (!"da".equals(type)) {
                    return new CommonResult().validateFailed("资产类型错误");
                } else {
                    columns1 = new String[]{"assetNum", "pactName", "bigNum", "smallNum", "basicsName", "specifica", "brand", "number", "startMoney", "money", "construcUnit", "buildUnit", "startTime", "stopTime", "planYear", "place", "remark", "images", "createTime"};
                    outDir = this.uploadFolderda + System.currentTimeMillis() + "/";
                }
                File outFile = new File(outDir);
                if (!outFile.exists()) {
                    outFile.mkdirs();
                }
                String fileName = multipartFile.getOriginalFilename();
                String filePath = outDir + fileName;
                multipartFile.transferTo(new File(filePath));
                if (!fileName.substring(fileName.lastIndexOf(".")).equals(".zip")) {
                    return new CommonResult().validateFailed("上传文件类型错误");
                }
                ZipOrRar zipOrRar = UnZipAndRarUtil.unZip(filePath, outDir);
                if ("zn".equals(type)) {
                    List columns = new ArrayList();
                    columns.add(columns1);
                    columns.add(columns2);
                    str = deZnCompress(zipOrRar, columns, projectNo, inputNum, sectionName, sectionId, companyId);
                } else if ("jc".equals(type)) {
                    str = deJcCompress(zipOrRar, columns1, projectNo, inputNum, sectionName, sectionId, companyId);
                } else if ("da".equals(type)) {
                    str = deDaCompress(zipOrRar, columns1, projectNo, inputNum, companyId);
                }
                if ("上传成功".equals(str)) {
                    UnZipAndRarUtil.delete(new File(outDir));
                    return new CommonResult().success();
                }
            }
        }
        return new CommonResult().validateFailed(str);
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Transactional(rollbackFor = {Exception.class})
    public String deZnCompress(ZipOrRar zipOrRar, List<String[]> columns, String projectNo, String inputNum, String sectionName, Integer sectionId, Integer companyId) throws Exception {
        Hyperlink link;
        String outPath = zipOrRar.getFilePath();
        zipOrRar.getRelativePaths();
        Sheet[] sheets = UnZipAndRarUtil.readZnExcel(outPath + zipOrRar.getExcelName());
        String sheet1 = "";
        String sheet2 = "";
        for (int i = 0; i < sheets.length; i++) {
            Sheet sheet = sheets[i];
            int lastRowNum = sheet.getLastRowNum();
            int lastCellNum = columns.get(i).length;
            Object cellData = null;
            Map<String, Object> map = new LinkedHashMap<>();
            int resutlNum = 0;
            for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (!StrUtil.isEmpty(UnZipAndRarUtil.getStringValue(row.getCell(1)))) {
                    System.out.println(row);
                    for (int cellNum = 0; cellNum < lastCellNum; cellNum++) {
                        Cell cell = row.getCell(cellNum);
                        if (i == 0) {
                            if (cellNum == 24) {
                                cellData = Integer.valueOf(Integer.parseInt(UnZipAndRarUtil.getStringValue(cell)));
                            } else {
                                cellData = UnZipAndRarUtil.getStringValue(cell);
                            }
                        } else if (i == 1) {
                            if (cellNum == 22) {
                                cellData = Integer.valueOf(Integer.parseInt(UnZipAndRarUtil.getStringValue(cell)));
                            } else {
                                cellData = UnZipAndRarUtil.getStringValue(cell);
                            }
                        }
                        if (cell == null || (link = cell.getHyperlink()) == null) {
                            map.put(columns.get(i)[cellNum], cellData);
                        } else {
                            map.put(columns.get(i)[cellNum], link.getAddress());
                        }
                    }
                    try {
                        Hardwit hardwit = new Hardwit();
                        Softwit softwit = new Softwit();
                        Images images1 = new Images();
                        String images = null;
                        if (i == 0) {
                            MapUtil.mapToZnh(map, hardwit);
                            images = hardwit.getImages();
                            hardwit.setBuildNum(projectNo);
                            hardwit.setStartMoney(hardwit.getStartMoney().replace(",", ""));
                            hardwit.setDataLabelsId(DataLabelsComponent.toEnableId);
                            String BigNum = this.witbigService.findOneByName("zn", hardwit.getBigNum());
                            if (StringUtils.isEmpty(BigNum)) {
                                return "硬件资产大类查询失败";
                            }
                            hardwit.setBigNum(BigNum);
                            String SmallNum = this.witsmallService.findOneByName("zn", hardwit.getBigNum(), hardwit.getSmallNum());
                            if (StringUtils.isEmpty(SmallNum)) {
                                return "硬件资产小类查询失败";
                            }
                            hardwit.setSmallNum(SmallNum);
                            String ConstrucUnit = this.unitService.findOneByName("zd", hardwit.getConstrucUnit());
                            if (StringUtils.isEmpty(ConstrucUnit)) {
                                return "硬件建设单位查询失败";
                            }
                            hardwit.setConstrucUnit(ConstrucUnit);
                            String BuildUnit = this.unitService.findOneByName("zn", hardwit.getBuildUnit());
                            if (StringUtils.isEmpty(BuildUnit)) {
                                return "硬件承建单位查询失败";
                            }
                            hardwit.setBuildUnit(BuildUnit);
                            String MaintainUnit = this.unitService.findOneByName("zn", hardwit.getMaintainUnit());
                            if (StringUtils.isEmpty(MaintainUnit)) {
                                return "硬件运维单位查询失败";
                            }
                            hardwit.setMaintainUnit(MaintainUnit);
                            hardwit.setAssetsStatus("znh1");
                            hardwit.setUserId(Integer.valueOf(inputNum));
                            hardwit.setImages(outPath + images);
                            hardwit.setApprovalStatus("0");
                            hardwit.setSectionName(sectionName);
                            hardwit.setSectionId(sectionId);
                            hardwit.setCompanyId(companyId);
                            if (this.hardwitService.insert(hardwit) > 0) {
                                resutlNum++;
                            }
                            if (!"".equals(images) && images != null) {
                                images1.setType("znh");
                                images1.setAssetsId(hardwit.getId().intValue());
                                images1.setIcon(createFileItem(hardwit.getImages()));
                            }
                        } else if (i == 1) {
                            MapUtil.mapToZns(map, softwit);
                            images = softwit.getImages();
                            softwit.setBuildNum(projectNo);
                            softwit.setBigNum("08");
                            softwit.setSmallNum("01");
                            softwit.setDataLabelsId(DataLabelsComponent.toEnableId);
                            softwit.setStartMoney(softwit.getStartMoney().replace(",", ""));
                            String ConstrucUnit2 = this.unitService.findOneByName("zd", softwit.getConstrucUnit());
                            if (StringUtils.isEmpty(ConstrucUnit2)) {
                                return "软件建设单位查询失败";
                            }
                            softwit.setConstrucUnit(ConstrucUnit2);
                            String BuildUnit2 = this.unitService.findOneByName("zn", softwit.getBuildUnit());
                            if (StringUtils.isEmpty(BuildUnit2)) {
                                return "软件承建单位查询失败";
                            }
                            softwit.setBuildUnit(BuildUnit2);
                            String MaintainUnit2 = this.unitService.findOneByName("zn", softwit.getMaintainUnit());
                            if (StringUtils.isEmpty(MaintainUnit2)) {
                                return "软件运维单位查询失败";
                            }
                            softwit.setMaintainUnit(MaintainUnit2);
                            softwit.setAssetsStatus("zns1");
                            softwit.setUserId(Integer.valueOf(inputNum));
                            softwit.setImages(outPath + images);
                            softwit.setApprovalStatus("0");
                            softwit.setSectionName(sectionName);
                            softwit.setSectionId(sectionId);
                            softwit.setCompanyId(companyId);
                            if (this.softwitService.insert(softwit) > 0) {
                                resutlNum++;
                            }
                            if (!"".equals(images) && images != null) {
                                images1.setType("zns");
                                images1.setAssetsId(softwit.getId().intValue());
                                images1.setIcon(createFileItem(softwit.getImages()));
                            }
                        }
                        if (!"".equals(images) && images != null) {
                            images1.setProjectNo(projectNo);
                            if (i == 0) {
                                images1.setProjectName(hardwit.getPactName().replace(" ", ""));
                            } else {
                                images1.setProjectName(softwit.getPactName().replace(" ", ""));
                            }
                            String imagesPath = this.imagesService.storeFile(images1);
                            images1.setCreateDate(DateUtil.now());
                            images1.setConnect(imagesPath);
                            this.imagesService.add(images1);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return e.toString();
                    }
                }
            }
            if (lastRowNum < resutlNum) {
                return "上传失败";
            }
            if (i == 0) {
                sheet1 = "上传成功";
            } else if (i == 1) {
                sheet2 = "上传成功";
            }
        }
        if (sheets.length > 1) {
            if (!"上传成功".equals(sheet1) || !"上传成功".equals(sheet2)) {
                return "上传失败";
            }
            return "上传成功";
        } else if ("上传成功".equals(sheet1)) {
            return "上传成功";
        } else {
            return "上传失败";
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Transactional(rollbackFor = {Exception.class})
    public String deDaCompress(ZipOrRar zipOrRar, String[] columns, String projectNo, String inputNum, Integer companyId) throws Exception {
        Object cellData;
        Hyperlink link;
        String outPath = zipOrRar.getFilePath();
        zipOrRar.getRelativePaths();
        Sheet sheet = UnZipAndRarUtil.readExcel(outPath + zipOrRar.getExcelName());
        int lastRowNum = sheet.getLastRowNum();
        System.out.println(lastRowNum);
        int lastCellNum = columns.length;
        Map<String, Object> map = new LinkedHashMap<>();
        DailyAsset dailyAsset = new DailyAsset();
        int resutlNum = 0;
        for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
            Row row = sheet.getRow(rowNum);
            if (!StrUtil.isEmpty(UnZipAndRarUtil.getStringValue(row.getCell(1)))) {
                for (int cellNum = 0; cellNum < lastCellNum; cellNum++) {
                    Cell cell = row.getCell(cellNum);
                    if (cellNum == 7 || cellNum == 9 || cellNum == 14) {
                        System.out.println(rowNum + "+" + UnZipAndRarUtil.getStringValue(cell) + "+" + cellNum);
                        cellData = Integer.valueOf(Integer.parseInt(UnZipAndRarUtil.getStringValue(cell)));
                    } else {
                        cellData = UnZipAndRarUtil.getStringValue(cell);
                    }
                    if (cell == null || (link = cell.getHyperlink()) == null) {
                        map.put(columns[cellNum], cellData);
                    } else {
                        map.put(columns[cellNum].toString(), link.getAddress());
                    }
                }
                try {
                    System.out.println(map);
                    MapUtil.mapToDa(map, dailyAsset);
                    String images = dailyAsset.getImages();
                    String type = this.dailyProjectService.findTypeById(Integer.valueOf(Integer.parseInt(projectNo)));
                    dailyAsset.setBuildNum(projectNo);
                    dailyAsset.setStartMoney(dailyAsset.getStartMoney().replace(",", ""));
                    dailyAsset.setDataLabelsId(DataLabelsComponent.toEnableId);
                    String BigNum = this.witbigService.findOneByName(type, dailyAsset.getBigNum());
                    if (StringUtils.isEmpty(BigNum)) {
                        return "资产大类查询失败";
                    }
                    dailyAsset.setBigNum(BigNum);
                    String SmallNum = this.witsmallService.findOneByName(type, dailyAsset.getBigNum(), dailyAsset.getSmallNum());
                    if (StringUtils.isEmpty(SmallNum)) {
                        return "资产小类查询失败";
                    }
                    dailyAsset.setSmallNum(SmallNum);
                    String construcUnit = this.unitService.findOneByName("zd", dailyAsset.getConstrucUnit());
                    if (StringUtils.isEmpty(construcUnit)) {
                        return "建设单位查询失败";
                    }
                    dailyAsset.setConstrucUnit(construcUnit);
                    String buildcUnit = this.unitService.findOneByName(type, dailyAsset.getBuildUnit());
                    if (StringUtils.isEmpty(buildcUnit)) {
                        return "承建单位查询失败";
                    }
                    dailyAsset.setBuildUnit(buildcUnit);
                    dailyAsset.setUserId(Integer.valueOf(inputNum));
                    dailyAsset.setImages(outPath + images);
                    if (this.dailyAssetsService.add(dailyAsset) > 0) {
                        resutlNum++;
                    }
                    if (!"".equals(images) && images != null) {
                        Images images1 = new Images();
                        images1.setProjectNo(projectNo);
                        images1.setType("da");
                        images1.setAssetsId(dailyAsset.getId().intValue());
                        images1.setIcon(createFileItem(dailyAsset.getImages()));
                        images1.setProjectName(dailyAsset.getPactName().replace(" ", ""));
                        String imagesPath = this.imagesService.storeFile(images1);
                        images1.setCreateDate(DateUtil.now());
                        images1.setConnect(imagesPath);
                        this.imagesService.add(images1);
                    }
                } catch (Exception e) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    e.printStackTrace();
                    return e.toString();
                }
            }
        }
        return "上传成功";
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Transactional(rollbackFor = {Exception.class})
    public String deJcCompress(ZipOrRar zipOrRar, String[] columns, String projectNo, String inputNum, String sectionName, Integer sectionId, Integer companyId) throws Exception {
        Object cellData;
        Hyperlink link;
        String outPath = zipOrRar.getFilePath();
        zipOrRar.getRelativePaths();
        Sheet sheet = UnZipAndRarUtil.readExcel(outPath + zipOrRar.getExcelName());
        int lastRowNum = sheet.getLastRowNum();
        int lastCellNum = columns.length;
        Map<String, Object> map = new LinkedHashMap<>();
        int resutlNum = 0;
        for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
            Basics basics = new Basics();
            Row row = sheet.getRow(rowNum);
            if (!StrUtil.isEmpty(UnZipAndRarUtil.getStringValue(row.getCell(1)))) {
                for (int cellNum = 0; cellNum < lastCellNum; cellNum++) {
                    Cell cell = row.getCell(cellNum);
                    if (cellNum == 8 || cellNum == 10 || cellNum == 15 || cellNum == 19) {
                        cellData = Integer.valueOf(Integer.parseInt(UnZipAndRarUtil.getStringValue(cell)));
                    } else {
                        cellData = UnZipAndRarUtil.getStringValue(cell);
                    }
                    if (cell == null || (link = cell.getHyperlink()) == null) {
                        map.put(columns[cellNum], cellData);
                    } else {
                        String fileAddr = link.getAddress();
                        System.out.println(fileAddr);
                        map.put(columns[cellNum].toString(), fileAddr);
                    }
                }
                try {
                    MapUtil.mapToJc(map, basics);
                    String images = basics.getImages();
                    basics.setBuildNum(projectNo);
                    basics.setStartMoney(basics.getStartMoney().replace(",", ""));
                    String BigNum = this.witbigService.findOneByName("jc", basics.getBigNum());
                    if (StringUtils.isEmpty(BigNum)) {
                        return "资产大类查询失败";
                    }
                    basics.setBigNum(BigNum);
                    String SmallNum = this.witsmallService.findOneByName("jc", basics.getBigNum(), basics.getSmallNum());
                    if (StringUtils.isEmpty(SmallNum)) {
                        return "资产小类查询失败";
                    }
                    basics.setSmallNum(SmallNum);
                    String construcUnit = this.unitService.findOneByName("zd", basics.getConstrucUnit());
                    if (StringUtils.isEmpty(construcUnit)) {
                        return "建设单位查询失败";
                    }
                    basics.setConstrucUnit(construcUnit);
                    String buildcUnit = this.unitService.findOneByName("jc", basics.getBuildUnit());
                    if (StringUtils.isEmpty(buildcUnit)) {
                        return "承建单位查询失败";
                    }
                    basics.setBuildUnit(buildcUnit);
                    basics.setDataLabelsId(DataLabelsComponent.toEnableId);
                    basics.setAssetsStatus("jc1");
                    basics.setUserId(Integer.valueOf(inputNum));
                    basics.setImages(outPath + images);
                    basics.setApprovalStatus("0");
                    basics.setSectionName(sectionName);
                    basics.setSectionId(sectionId);
                    basics.setCompanyId(companyId);
                    if (this.basicsService.insert(basics) > 0) {
                        resutlNum++;
                    }
                    if (!"".equals(images) && images != null) {
                        Images images1 = new Images();
                        images1.setProjectNo(projectNo);
                        images1.setType("jc");
                        images1.setAssetsId(basics.getId().intValue());
                        images1.setIcon(createFileItem(basics.getImages()));
                        images1.setProjectName(basics.getPactName().replace(" ", ""));
                        String imagesPath = this.imagesService.storeFile(images1);
                        images1.setCreateDate(DateUtil.now());
                        images1.setConnect(imagesPath);
                        this.imagesService.add(images1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return e.toString();
                }
            }
        }
        return "上传成功";
    }

    public MultipartFile createFileItem(String filePath) {
        FileInputStream inputStream = null;
        try {
            MultipartFile multipartFile = null;
            inputStream = null;
            try {
                File pdfFile = new File(filePath);
                inputStream = new FileInputStream(pdfFile);
                multipartFile = new MockMultipartFile("file", pdfFile.getName(), "text/plain", IOUtils.toByteArray(inputStream));
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (Exception e3) {
                        e3.printStackTrace();
                    }
                }
            }
            return multipartFile;
        } catch (Throwable th) {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e4) {
                    e4.printStackTrace();
                    throw th;
                }
            }
            throw th;
        }
    }
}